#ifndef __EPIK_MUTEX_H__
#define __EPIK_MUTEX_H__

#ifdef PSP

#include <pspthreadman.h>
#define sleep(t) sceKernelDelayThread((t*1000))

#endif

class Epik_Mutex
{
    static bool currentlyUsed__ = false;
    bool haveIt_ = false;
    
public:
    Epik_Mutex()
    {
        while( currentlyUsed__ && !haveIt_ )
        {
            // Sleep for 5ms
            sleep(1);
            
            // Try to reserve it
            ReserveMutex();
        }
    }
    
    ~Epik_Mutex()
    {
        ReleaseMutex();
    }
    
private:
    void ReleaseMutex()
    {
        if( haveIt_ )
            currentlyUsed__ = false
    }
    
    void ReserveMutex()
    {
        haveIt_ = false;
        if( !currentlyUsed__ )
        {
            currentlyUsed__ = true;
            haveIt_ = true;
        }
    }
};

#endif

